import {anyObj} from "../lib/lib";

/* 
* 在有些人的返回信息中,有的使用msg有的使用message
* 除此之外在没有这些信息时,我们也想自己添加自定义的信息,此方法用来这种需求
 */
function formatMsg (data: anyObj, str: string) {
  let _str
  try {
    _str = data.msg || data.message || str
  } catch (error) {
    console.error(error)
    _str = str
  }
  return _str
}
interface toastParam {
  title: string
  icon?: string
  image?: string
  duration?: number
  mask?: boolean
  success?(): void
  fail?(err: Error): void
  complete?(): void
}
interface $wx {
  showToast (params: toastParam): any
}

declare var wx: $wx

// 信息方法,修饰
let msg = (function () {
  function msg (data: anyObj, str: string, {
    icon = "none"
  } = {}) {
    wx.showToast({
      icon,
      title: formatMsg(data, str)
    })
  }
  msg.success = function (data: anyObj, str: string) {
    msg(data, str, {
      icon: 'success'
    })
  }
  msg.error = function (data: anyObj, str: string) {
    msg(data, str)
  }
  return msg
})()
export default formatMsg

export {
  formatMsg,
  msg
}
